package cube.io.file;

import java.io.FileInputStream;

import cube.data.util.DataUtil;

/**
 * 文件读取工具
 * 
 * @author abiao
 */
public abstract class FileReader {

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 每次读取字节数
     */
    private int readSize = 1024;

    /**
     * 文件读取工具
     */
    public FileReader(String fileName) {
        this.fileName = fileName;
    }

    /**
     * 开始读取数据
     */
    public void start() {
        try {
            byte[] buff = new byte[readSize];
            FileInputStream fis = new FileInputStream(fileName);
            int len;
            while ((len = fis.read(buff)) != -1) {
                this.read(DataUtil.copy(buff, 0, len));
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取文件数据
     */
    public abstract void read(byte[] data);

    /**
     * 设置每次读取大小
     */
    public FileReader setReadSize(int readSize) {
        this.readSize = readSize;
        return this;
    }
}
